﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class FormAllLineLength : System.Windows.Forms.Form
	{
		public FormAllLineLength(bool isUp)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			base.Location = new System.Drawing.Point(100, 100);
			this.isUp = isUp;
			this.radioButtonUp.Checked = true;
			this.SJrB90.Checked = true;
			this.textBoxAngle.Text = "90";
		}

		private void isUpOrDown()
		{
			if (this.radioButtonDown.Checked)
			{
				this.isUp = false;
				return;
			}
			this.isUp = true;
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void FormAllLineLength_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				FormAllLineLength.SendMessage(DirectAutoRoundBeam.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void FormAllLineLength_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			FormAllLineLength.SendMessage(DirectAutoRoundBeam.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			FormAllLineLength.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormAllLineLength.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void FormAllLineLength_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void FormAllLineLength_FormClosing(object sender, FormClosingEventArgs e)
		{
		}

		private void FormAllLineLength_Leave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				FormAllLineLength.SendMessage(DirectAutoRoundBeam.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void radioButtonUp_CheckedChanged(object sender, EventArgs e)
		{
			this.isUpOrDown();
		}

		private void radioButtonDown_CheckedChanged(object sender, EventArgs e)
		{
			this.isUpOrDown();
		}

		private void radioAngleChange()
		{
			if (this.SJrB15.Checked)
			{
				this.angle = 0.26179938779914941;
				this.textBoxAngle.Enabled = false;
				return;
			}
			if (this.SJrB30.Checked)
			{
				this.angle = 0.52359877559829882;
				this.textBoxAngle.Enabled = false;
				return;
			}
			if (this.SJrB45.Checked)
			{
				this.angle = 0.78539816339744828;
				this.textBoxAngle.Enabled = false;
				return;
			}
			if (this.SJrB60.Checked)
			{
				this.angle = 1.0471975511965976;
				this.textBoxAngle.Enabled = false;
				return;
			}
			if (this.SJrB90.Checked)
			{
				this.angle = Math.PI*.5;
				this.textBoxAngle.Enabled = false;
				return;
			}
			if (this.radioButton3.Checked)
			{
				this.textBoxAngle.Enabled = true;
				if (this.textBoxAngle.Text != "")
				{
					this.angle = Convert.ToDouble(this.textBoxAngle.Text.Trim()) * Math.PI / 180.0;
				}
			}
		}

		private void SJrB15_CheckedChanged(object sender, EventArgs e)
		{
			this.radioAngleChange();
		}

		private void SJrB30_CheckedChanged(object sender, EventArgs e)
		{
			this.radioAngleChange();
		}

		private void SJrB45_CheckedChanged(object sender, EventArgs e)
		{
			this.radioAngleChange();
		}

		private void SJrB60_CheckedChanged(object sender, EventArgs e)
		{
			this.radioAngleChange();
		}

		private void SJrB90_CheckedChanged(object sender, EventArgs e)
		{
			this.radioAngleChange();
		}

		private void radioButton3_CheckedChanged(object sender, EventArgs e)
		{
			this.radioAngleChange();
		}

		private void textBoxAngle_Validated(object sender, EventArgs e)
		{
			if (!new Regex("^[0-9]+(.[0-9]{1,3})?$").IsMatch(this.textBoxAngle.Text))
			{
				YJKMessageBox.Show("角度请输入数字,且必须为正！");
				this.textBoxAngle.Focus();
				this.textBoxAngle.SelectAll();
				return;
			}
			if (this.regex.IsMatch(this.textBoxAngle.Text) && Convert.ToDouble(this.textBoxAngle.Text.Trim()) > 90.0)
			{
				YJKMessageBox.Show("角度不能大于90度！");
				this.textBoxAngle.Focus();
				this.textBoxAngle.SelectAll();
				return;
			}
		}

		public bool isUp = true;

		public double angle = Math.PI*.5;

		private Regex regex = new Regex("^[0-9]+(.[0-9]{1,3})?$");
	}
}
